Declaring global variables for resource or configuration and using them

Declaring/using resource-global variables

How to declare →global variables for a →resource and use them in the ST-code:

  1. Open a PLC-object.

  2. Search for the following line in the opened editor for the PLC-object:

    Syntax
    RESOURCE resource-name ON platform-name { ON_CHANNEL := channel-name }
    Example
    RESOURCE local ON BuiltInPlc { ON_CHANNEL := LocalChannel }
  3. After this line, enter the declaration of the resource-global variable:

    Syntax
    RESOURCE resource-name ON platform-name { ON_CHANNEL := channel-name }
     
    ( * optional_begin *) USING  Namespace_1;
    USING  Namespace_2; (* optional_end *)
     
    VAR_GLOBAL (* optional_begin *) CONSTANT RETAIN NON_RETAIN {DMA := 'string'} (* optional_end *)
      name_1, name_2, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : data-type|FB-type := initial-value;
      ...
    END_VAR
    Example
    RESOURCE local ON BuiltInPlc { ON_CHANNEL := LocalChannel }
    VAR_GLOBAL
      VALVE_POS AT %QW28 : INT;
    END_VAR

    (info) See "Declaration of global variables in ST" on the meaning of the syntax for VAR_GLOBAL ... END_VAR and possible options. There is an alternative to declare global variables within a global-object and to insert here just a reference
    (info) Moreover, it is here possible to declare global function block instance as well. This can be done as described under "Declaration of function block instances in ST".
    The USING namespace directive in front of VAR_GLOBAL ... END_VAR is optional. See "Namespaces in ST: usage" for details on this directive.
    Moreover, Neuron Power Engineer allows you to specify additional data for the declaration. See "Defining description, comment, JSON string or type for variables or data types" for details.

    If you are using EtherCAT fieldbuses that can be addressed via →EC-Master and →EC-EngineerNeuron Power Engineer is able to enter the declaration for you. But to make this happen, you have to follow the instructions under "Accessing hardware IOs via EC-Master and EC-Engineer of Acontis".

  4. Save the PLC-object: menu File – Save

  5. Access the declared resource-global variables in the context of →POUs by using →external variables:

    1. Open the object in which you want to declare the external variable

    2. In this object, declare an external variable – in particular within the declaration of the POU where the access is necessary. Access is possible within the declaration of the program, the declaration of a function block or the declaration of a function.
      See "Declaration of external variables in ST" on the ST-syntax required for the external variable. Information on the declaration of variables in a graphical editor is listed under "Declaring variables".

    3. In the object, use the external variable within the declaration of the POU (e.g. enter it within an →assignment).

    4. Save the object: menu File – Save

Declaring/using configuration-global variables

How to declare global variables for a →configuration and use them in the ST-code:

  1. Analogous to the declaration of resource-global variables (see above): Open a PLC-object.

  2. Search for the following line in the opened editor for the PLC-object:

    Syntax
    CONFIGURATION configuration-name
    Example
    CONFIGURATION LocalConfiguration
  3. After this line, enter the declaration of the configuration-global variable. This declaration is analogous to the declaration of resource-global variables (see above).

    Example
    CONFIGURATION LocalConfiguration
    VAR_GLOBAL
      VALVE_POS AT %QW28 : INT;
    END_VAR

    There is also an alternative to declare global variables within a global-object and to insert here just a reference.

  4. Save the PLC-object: menu File – Save

  5. Access the declared configuration-global variables in the context of →POUs by using →external variables as well. This access is analogous to the access of resource-global variables (see above).